﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Common;
using AutoFacUtils;
using CashFlow.Admin.Filters;
using CashFlow.Admin.Service;
using CashFlow.Admin.Common;
using CashFlow.Admin.Def;
using CashFlow.Admin.Models;
using CashFlow.Admin.Models.Result;
using CashFlow.Admin.Validator;

namespace CashFlow.Admin.Controllers.Shop
{
    [AdminFilter]
    public class AdminController : Controller
    {
        private IAdminService GetAdminService() { return AutoFacHelper.Get<IAdminService>(); }
        private IShopService GetShopService() { return AutoFacHelper.Get<IShopService>(); }

        // GET: Admin
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult List()
        {
            var service = GetAdminService();
            var ls = service.QueryAll2();
            var ls2 = ls.Select(x => Convert(x)).ToList();
            return MyCommonTool.CreateLayUITableResult(true, "", ls2);
        }

        [HttpPost]
        public ActionResult Delete(int adminId)
        {
            var service = GetAdminService();
            bool success = service.DeleteById(adminId);
            return MyCommonTool.CreateOperationResult(success, "", null);
        }

        [HttpPost]
        [ValidatorFilter(typeof(ResetAdminPwdModelValidator))]
        public ActionResult ResetPwd(ResetAdminPwdModel model)
        {
            bool success = false;
            string msg = "";

            var service = GetAdminService();
            success = service.ResetPwd(model.Pwd, model.NewPwd, model.AdminId);
            if (!success)
            {
                msg = "原密码错误！";
            }

            return MyCommonTool.CreateOperationResult(success, msg, null);
        }

        [HttpPost]
        public ActionResult GetShopList()
        {
            var service = GetShopService();
            var ls = service.QueryNotSelectedShopList();
            return MyCommonTool.CreateOperationResult(true, "", ls);
        }

        [HttpPost]
        [ValidatorFilter(typeof(AddAdminModelValidator))]
        public ActionResult AddAdmin(AddAdminModel model)
        {
            var service = GetAdminService();
            int result = service.AddAdminEntity(model.ShopManagerUserId
                , model.UserName
                , model.Pwd
                , model.Name);
            return MyCommonTool.CreateOperationResult(
                result == (int)EAddAdminResult.Success ? true : false
                , EnumTool.GetDescription<EAddAdminResult, int>(result)
                , null);
        }

        protected AdminTableInfoModel Convert(AdminShopInfoView view)
        {
            var model = new AdminTableInfoModel();
            model.Id = view.AdminInfo.Id;
            model.Name = view.AdminInfo.Name;
            model.UserName = view.AdminInfo.UserName;
            model.CreateTime = view.AdminInfo.CreateTime;
            model.IsAdmin = MyCommonTool.IsAdministrator(view.AdminInfo);
            if (view.ShopInfo != null && view.ShopInfo.Id != 0)
            {
                model.ShopManagerUserId = view.ShopInfo.ShopManagerUserId;
                model.ShopName = view.ShopInfo.Name;
            }

            return model;
        }
    }
}